package com.nit.lab.vote.service.system.impl;

import com.alibaba.fastjson.JSONObject;
import com.nit.lab.vote.dto.basic.UserDTO;
import com.nit.lab.vote.dto.system.AccountDTO;
import com.nit.lab.vote.dto.system.PermissionDTO;
import com.nit.lab.vote.dto.system.RoleDTO;
import com.nit.lab.vote.dto.system.TokenDTO;
import com.nit.lab.vote.dto.utils.OpResultDTO;
import com.nit.lab.vote.service.basic.UserService;
import com.nit.lab.vote.service.system.AccountService;
import com.nit.lab.vote.service.system.RoleService;
import com.nit.lab.vote.utils.JWTUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AccountServiceImpl implements AccountService {

    @Autowired(required = false)
    private UserService userService;
    @Autowired(required = false)
    private RoleService roleService;
    @Autowired(required = false)

    @Value(value = "${application.jwt.expire}")
    private Integer expire;

    @Override
    public JSONObject loginCheck(String phone, String loginPwd) throws Exception {
        JSONObject jsonObject = new JSONObject();
        TokenDTO tokenDTO = new TokenDTO();
        OpResultDTO opResult = new OpResultDTO();
        AccountDTO accountDTO = new AccountDTO();
        UserDTO userDto = userService.loginCheck(phone);
        if (userDto == null || !userDto.getPassword().equals(loginPwd)) {
            opResult.setResult(JSONObject.parseObject("{ isLogin: true }"));
            jsonObject.put("data", opResult);
            jsonObject.put("message", "账户或密码错误");
            jsonObject.put("status", 401);
        } else {
            accountDTO.setId(userDto.getUserId());
            accountDTO.setName(userDto.getUserName());
            accountDTO.setRoleId(String.valueOf(userDto.getRoleId()));
            // 生成签名DTO，包含账号ID，账户类型
            tokenDTO.setRoleId(userDto.getRoleId());
            tokenDTO.setUserId(userDto.getUserId());
            tokenDTO.setUserName(userDto.getUserName());
            accountDTO.setToken(JWTUtil.createSign(tokenDTO.toString(), expire));
            opResult.setResult(accountDTO);
            jsonObject.put("data", opResult);
            jsonObject.put("message", "");
            jsonObject.put("status", 200);
        }
        return jsonObject;
    }

    @Override
    public AccountDTO getInfo(String token) throws Exception {
        AccountDTO accountDTO = new AccountDTO();
        TokenDTO tokenDTO = JWTUtil.verifyToken(token);
        Integer roleId = tokenDTO.getRoleId();

        UserDTO userDTO = userService.getByUserId(tokenDTO.getUserId());
        accountDTO.setId(userDTO.getUserId());
        accountDTO.setUserName(userDTO.getUserName());
        accountDTO.setTelephone(userDTO.getPhone());
        accountDTO.setRoleId(String.valueOf(userDTO.getRoleId()));

        accountDTO.setToken(JWTUtil.createSign(tokenDTO.toString(), expire));
        List<PermissionDTO> listPermissionDto = roleService.listPermissionByRoleId(roleId);
        RoleDTO roleDTO = roleService.getRoleByRoleId(roleId);
        roleDTO.setPermissions(listPermissionDto);
        accountDTO.setRole(roleDTO);
        return accountDTO;
    }
}
